<?php
App::import('Sanitize');
class TasksController extends AppController {
	var $name = 'Tasks';

	function index() {
		$this->redirect('/');
		$this->Task->recursive = 0;
		$this->set('tasks', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
		var_dump($this->Task);
			$this->Session->setFlash(__('Invalid task', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->set('task', $this->Task->read(null, $id));
			$this->redirect('/');
		}
		$this->set('task', $this->Task->read(null, $id));
	}

	function add() {	
		if(!($this->Session->read('Auth.User'))){
			$this->Session->setFlash(__('Please, login first.', true));
			$this->redirect(array('controller' => 'users', 'action' => 'login'));
		} else {
			if (!empty($this->data)) {
			
		    	// sanitize any user-input data
		    	$this->Task->data = Sanitize::clean($this->data);
		    	
		    	// Run user-input task through iDunnit natural language parser
		    	$parsed_task = $this->_parse_task_and_date($this->Task->data['Task']['title']);
		    	
		    	// Check if the task contains a timestamp
		    	if(!$parsed_task['date']){
		    		$this->Session->setFlash(__('Sorry, we could not find a valid timestamp in your task. Please try again!', true));
		    	} else {
		    		// generate from submitted form fields first
		    		$this->Task->create();
		    	
		    		// Save the task logic here
		    		$this->data['Task']['title'] = implode(" ",$parsed_task['text']);
		    		$this->data['Task']['complete_on'] = date("Y-m-d H:i:s", $parsed_task['date']);
		    		if ($this->Task->save($this->data)) {
						$this->Session->setFlash(__('Your task has been saved', true));
						$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__('The task could not be saved. Please, try again.', true));
					}
		    	}
		    	
		    	/**
				$this->Task->create();
				
				if ($this->Task->save($this->data)) {
					$this->Session->setFlash(__('Your task has been saved', true));
					// $this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The task could not be saved. Please, try again.', true));
				}
				*/
			}
			$users = $this->Task->User->find('list');
			$this->set(compact('users'));
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid task', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Task->save($this->data)) {
				$id = $this->Auth->user('id');
				$this->Session->setFlash(__('The task has been saved', true));
	 			$this->redirect(array('controller'=>'users','action'=>'view/'.$id),null,true);
			} else {
				$this->Session->setFlash(__('The task could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Task->read(null, $id);
		}
		$users = $this->Task->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for task', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Task->delete($id)) {
			$this->Session->setFlash(__('Task deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Task was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	 *	Function to parse task and date separately from user-input task
	 *	@var String
	 *	@return Array
	 */
	function _parse_task_and_date($parse_task) {
		// Lowercase the string so excluded words match irrespective of case
		$parse_task = strtolower($parse_task);
		
		// exclude these words from the task
		$exclude = array('i', 'at', 'will');
		
		// create an array from user-input task
		$parse_array = explode(" ", $parse_task);
		
		// store Date Time information in this array
		$date = array();
		
		// store Task in this array
		$data['text'] = array();
		
		// Count the number of words in user submitted task
		$word_count = count($parse_array);
		
		for($i = 0; $i < $word_count; $i++) {
			// Not using this because && is a short-circuit operator! we don't need to parse excluded words
			// if (strtotime($parse_array[$i]) > 0 && !in_array($parse_array[$i], $exclude))
			
			// not parsing excluded words now :)
			if (!in_array($parse_array[$i], $exclude) && strtotime($parse_array[$i]) > 0) {
				$date[] = $parse_array[$i];
			} else {
				if(!in_array($parse_array[$i], $exclude))
				$data['text'][] = $parse_array[$i];
			}
		}
		$data['date'] = strtotime(implode(" ", $date));
		return $data;
	}
}
?>
